package com.qf.userweb.interceptor;

import com.qf.userweb.entity.User;
import com.qf.userweb.utils.AccessDefinedException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从请求对象中获取session
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        //判断
        if(user == null){
              throw new AccessDefinedException("没有权限，拒绝访问!");
        }
        return true;
    }

}
